package com.example.shopp_2301.car

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.CarEntity
import com.example.base.entity.ParamPreOrder
import com.example.base.entity.PreOrderEntity
import com.example.base.mvvm.BaseModel
import com.example.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class CarViewModel:BaseViewModel() {
    //返回值--购物车列表
    val carSuccess = MutableLiveData<BaseModel<CarEntity>>()
    val carFail = MutableLiveData<String>()
    fun car(isValid:String,limit:String,page:String){
        viewModelScope.launch {
            try {
                carSuccess.value = apiServer.car(isValid, limit, page)
            }catch (e:Exception){
                carFail.value = e.message
            }
        }
    }
    //返回值--修改购物车
    val updateSuccess = MutableLiveData<BaseModel<String?>>()
    val updateFail = MutableLiveData<String>()
    fun update(id:Int,number:Int){
        viewModelScope.launch {
            try {
                updateSuccess.value = apiServer.update(id, number)
            }catch (e:Exception){
                updateFail.value = e.message
            }
        }
    }
    //返回值---删除购物车
    val deleteSuccess = MutableLiveData<BaseModel<String?>>()
    val deleteFail = MutableLiveData<String>()
    fun delete(ids:Array<Int>){
        viewModelScope.launch {
            try {
                deleteSuccess.value = apiServer.delete(ids)
            }catch (e:Exception){
                deleteFail.value = e.message
            }
        }
    }
    //返回值---预下单
    val preSuccess = MutableLiveData<BaseModel<PreOrderEntity>>()
    val preFail = MutableLiveData<String>()
    fun preOrder(body:ParamPreOrder){
        viewModelScope.launch {
            try {
                preSuccess.value = apiServer.preOrder(body)
            }catch (e:Exception){
                preFail.value = e.message
            }
        }
    }

}